﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

/// <summary>
/// PublicDropdownList 的摘要说明
/// </summary>
public class PublicDropdownList
{
    #region 绑定DropDownList控件
    //绑定DropDownList控件
    public static void BindDropDownListData(DropDownList _CDropDownList, DataSet _Ds, string strKey, string strValue)
    {
        _CDropDownList.Items.Clear();
        ListItem lit = new ListItem();
        lit.Value = "";
        lit.Text = "--请选择--";
        _CDropDownList.Items.Add(lit);

        for (int i = 0; i < _Ds.Tables[0].Rows.Count; i++)
        {
            _CDropDownList.Items.Add(new ListItem(_Ds.Tables[0].Rows[i][strKey].ToString()));
            _CDropDownList.Items[i + 1].Value = _Ds.Tables[0].Rows[i][strValue].ToString();
        }
    }

    public static void BindDropDownList(DropDownList _CDropDownList, DataSet _Ds, string strKey, string strNum, string strValue)
    {
        _CDropDownList.Items.Clear();
        ListItem lit = new ListItem();
        lit.Value = "";
        lit.Text = "--请选择--";
        _CDropDownList.Items.Add(lit);

        for (int i = 0; i < _Ds.Tables[0].Rows.Count; i++)
        {
            _CDropDownList.Items.Add(new ListItem(_Ds.Tables[0].Rows[i][strNum].ToString() + "" + _Ds.Tables[0].Rows[i][strKey].ToString()));
            _CDropDownList.Items[i + 1].Value = _Ds.Tables[0].Rows[i][strValue].ToString();
        }
    }
    //重载绑定DropDownList控件
    public static void BindDropDownListData(DropDownList _CDropDownList, DataSet _Ds, string strKey, string strValue, int intProgressionNum)
    {
        for (int i = 0; i < _Ds.Tables[0].Rows.Count; i++)
        {
            _CDropDownList.Items.Add(new ListItem(_Ds.Tables[0].Rows[i][strValue].ToString()));
            _CDropDownList.Items[i + intProgressionNum].Value = _Ds.Tables[0].Rows[i][strKey].ToString();
        }
    }
    #endregion
    //利用xml进行数据的绑定
    public static void BindDropDownListWithXML(DropDownList _CDropDownList, string XMLFile)
    {
        _CDropDownList.Items.Clear();
        ListItem lit = new ListItem();
        lit.Value = "0";
        lit.Text = "--请选择--";
        _CDropDownList.Items.Add(lit);
        DataSet _dsDataSet = new DataSet();
        _dsDataSet.ReadXml(XMLFile);
        for (int j = 0; j < _dsDataSet.Tables[0].Rows.Count - 1; j++)
        {
            _CDropDownList.Items.Add(new ListItem(_dsDataSet.Tables[0].Rows[j + 1]["Text"].ToString()));
            _CDropDownList.Items[j + 1].Value = _dsDataSet.Tables[0].Rows[j + 1]["Value"].ToString();
        }
    }
    public static void BindDropDownListWithHashtable(DropDownList _CDropDownList, Hashtable hs)
    {
        _CDropDownList.Items.Clear();
        ListItem lit = new ListItem();
        lit.Value = "";
        lit.Text = "--请选择--";
        _CDropDownList.Items.Add(lit);
        System.Collections.IDictionaryEnumerator IDenum = hs.GetEnumerator();

        int j = 0;
        while (IDenum.MoveNext())
        {
            j = j + 1;
            _CDropDownList.Items.Add(new ListItem(IDenum.Value.ToString()));
            _CDropDownList.Items[j].Value = IDenum.Key.ToString();


        }
    }
    public static void BindDropDownListWithHashtableNoChoise(DropDownList _CDropDownList, Hashtable hs)
    {
        _CDropDownList.Items.Clear();
        ListItem lit = new ListItem();
        //			lit.Value = "0";
        //			lit.Text = "--请选择--";
        //			_CDropDownList.Items.Add(lit);
        System.Collections.IDictionaryEnumerator IDenum = hs.GetEnumerator();

        int j = 0;
        while (IDenum.MoveNext())
        {
            _CDropDownList.Items.Add(new ListItem(IDenum.Value.ToString()));
            _CDropDownList.Items[j].Value = IDenum.Key.ToString();
            j = j + 1;

        }
    }




}

